#include "EnemyManager.h"
#include <iostream>


EnemyManager::EnemyManager(OgreOde::World* world)
{
	//this->scene_manager = scene_manager;
    this->world = world;
	mSceneMgr = world->getSceneManager();
    mSpace = world->getDefaultSpace();
	mWorld = world;
	/* start with a number of enemies */
	/*int start_num = Ogre::Math::RangeRandom(5, 15);
	for( int i = 0; i <= start_num; i++ )
	{
		create_enemy();
	}*/
	BoidManager::createSingleton();

	kill_counter = 0;
}

void EnemyManager::update(Vector3 ogre_pos)
{/*
	CraftList::iterator iter = mAirCraftBoids.begin();
		while (iter != mAirCraftBoids.end())
		{
			float squaredDistance = iter->first->getPosition().squaredLength();
			if (squaredDistance > 2500.0f)
			{
				iter->second->mWeight = 1.0f;
			}
			else
			{
				iter->second->mWeight = 0.0f;
			}

			iter++;
		}*/

	for( iter = enemies.begin(); iter != enemies.end(); iter++ )
	{
		int x = 0, y = 0, z = 0;
		
		/* xaxis */
		if( ogre_pos.x > (*iter)->get_position().x ) x = 1;
		else if( ogre_pos.x < (*iter)->get_position().x ) x = -1;

		/* yaxis */
		if( ogre_pos.y > (*iter)->get_position().y ) y = 1;
		else if( ogre_pos.y < (*iter)->get_position().y ) y = -1;

		/* zaxis */
		if( ogre_pos.z > (*iter)->get_position().z ) z = 1;
		else if( ogre_pos.z < (*iter)->get_position().z ) z = -1;
        //printf("x: %i y: %i z: %i", x, y, z);
		(*iter)->update_position(Vector3(x, y, z));
	}

    if( (int) Ogre::Math::RangeRandom(0, 500) == 1 ) {
        Vector3 position;

		int start_num = Ogre::Math::RangeRandom(5, 10);

        if ((int)Ogre::Math::RangeRandom(0, 2) == 1) {
            position = Vector3( ogre_pos.x + (int)Ogre::Math::RangeRandom(1000, 15000),
                              ogre_pos.y + (int)Ogre::Math::RangeRandom(1000, 15000),
                              ogre_pos.z + (int)Ogre::Math::RangeRandom(1000, 15000));
        } else {
            position = Vector3( ogre_pos.x - (int)Ogre::Math::RangeRandom(1000, 15000),
                              ogre_pos.y - (int)Ogre::Math::RangeRandom(1000, 15000),
                              ogre_pos.z - (int)Ogre::Math::RangeRandom(1000, 15000));
        }
		create_enemy(position, ogre_pos);
		/*for( int i = 0; i <= start_num; i++ )
		{
            Vector3 position2 = Vector3( (int)Ogre::Math::RangeRandom(100, 1000),
                              (int)Ogre::Math::RangeRandom(100, 1000),
                              (int)Ogre::Math::RangeRandom(100, 1000));
        	create_enemy(position + position2, ogre_pos);
			
			if (i == 1)
				ogre_pos = Vector3(0,0,0);
		}*/
    } 
}


void EnemyManager::create_enemy (Vector3 position, Vector3 ogre_pos)
{
	Enemy* enemy = new Enemy("enemy_" + StringConverter::toString(counter), world, position, mAirCraftBoids);
	
	enemies.push_back(enemy);/*

	Vector3 size(50.0,50.0,50.0);
	mGeom = (OgreOde::Geometry*)new OgreOde::BoxGeometry(size, mWorld, mSpace);
	mName = "enemy_" + StringConverter::toString(counter);
	enemy_node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
	enemy_entity = mSceneMgr->createEntity("ship" + mName, "razor.mesh");
	enemy_node->attachObject(enemy_entity);
	mBody = new OgreOde::Body(mWorld, mName);
    enemy_node->attachObject(mBody);
    enemy_node->setScale(size.x * 0.02,size.y * 0.02,size.z * 0.02);
    mGeom->setBody(mBody);
    enemy_entity->setUserObject(mGeom);
    mBody->setPosition(position);

	BoidAirCraftPtr AirCraftBoid(new BoidAirCraft(mBody,
		5.5f, // thrust
		0.3f, // lift
		0.25f, // minDrag
		0.8f, // maxDrag
		-0.15f, // gravity
		Ogre::Radian(0.05f), // max pitch
		Ogre::Radian(0.05f) // max roll
		));

	AirCraftBoid->setSightRange(10.0f, 20.0f, Ogre::Math::PI * 0.75f);

	BehaviorSeparation *separationBehavior = (BehaviorSeparation *)AirCraftBoid->addBehavior(BEHAVIOR_SEPARATION);
	separationBehavior->setDetectionRange(3.0f, 5.0f, (Ogre::Math::PI * 0.75f));
	separationBehavior->mWeight = 3.0f;

	BehaviorCohesion *cohesionBehavior = (BehaviorCohesion *)AirCraftBoid->addBehavior(BEHAVIOR_COHESION);
	cohesionBehavior->setDetectionRange(3.0f, 10.0f, Ogre::Math::HALF_PI);

	BehaviorAlignment *alignmentBehavior = (BehaviorAlignment *)AirCraftBoid->addBehavior(BEHAVIOR_ALIGNMENT);
	alignmentBehavior->setDetectionRange(3.0f, 8.0f, Ogre::Math::HALF_PI / 2.0f);

	BehaviorSeek *seekBehavior = (BehaviorSeek *)AirCraftBoid->addBehavior(BEHAVIOR_SEEK);
	seekBehavior->mWeight = 0.0f;
	seekBehavior->setTarget(ogre_pos);
	mAirCraftBoids.push_back(CraftBehaviorPair(AirCraftBoid, seekBehavior));

*/
	counter++;
}

void EnemyManager::delete_enemy (OgreOde::Geometry *del) {
		OgreOde::Body *body = del->getBody();
		Ogre::String name = body->getName();
	for( iter = enemies.begin(); iter != enemies.end(); iter++ )
	{
		if ((*iter)->get_name().compare(name) == 0) {
			printf("Delete enemy!\n");
			(*iter)->~ Enemy();
			enemies.erase(iter);
		}
	}/*
	try {
printf("\nNAME:\n");
		OgreOde::Body *body = del->getBody();
		Ogre::String name = body->getName();

		CraftList::iterator iter = mAirCraftBoids.begin();
		while (iter != mAirCraftBoids.end())
		{
			Ogre::String name2 = iter->first->getName();
			if (name2.compare(name) == 0) {
				printf("\n\nDUUUUUDE\n");
				mAirCraftBoids.erase(iter);
				printf("\n\nDUUUUUDE\n");
			}
			iter++;
		}
		
		mSceneMgr->destroySceneNode(name);
		
		body->~ Body();
		del->~ Geometry();
	} 
	catch (...) 
	{ 
	//u dont have the node already registered in the scene manager 
	}*/
	kill_counter++;	
}
